package org.dprojects.iwebshare.filesystem;

import java.io.File;

import org.dprojects.iwebshare.ui.container.KalelFilesystemContainer;

import com.vaadin.data.util.FilesystemContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.terminal.Resource;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.ProgressIndicator;
import com.vaadin.ui.Tree;

public class SimpleFileSystemManagerImpl implements FileSystemManager {

	public void populate(String path,Tree tree, ProgressIndicator indicator) {
		
		FilesystemContainer c=new KalelFilesystemContainer(new File(path),true);
		
		tree.addContainerProperty(FilesystemContainer.PROPERTY_ICON, Resource.class, null);
		tree.addContainerProperty(FilesystemContainer.PROPERTY_NAME, String.class, null);
		 
		tree.setItemIconPropertyId(FilesystemContainer.PROPERTY_ICON);
		tree.setItemCaptionPropertyId(FilesystemContainer.PROPERTY_NAME);
		tree.setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);
		
		tree.setContainerDataSource(c);
		
		tree.addListener(new ItemClickEvent.ItemClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 5366945844015065921L;

			public void itemClick(ItemClickEvent event) {				
				System.out.println("item selected "+event.getItemId());
			}
		});
		
		indicator.setValue(new Float(1.0));
	}
	
	/*
	class WorkThread extends Thread {
		
		Tree tree;
		ProgressIndicator indicator;
		String path;
		
		public WorkThread(String path,Tree tree,ProgressIndicator indicator) {
			this.tree=tree;this.indicator=indicator;this.path=path;
		}
		
		public void run (){ 
			double current=0.0;
			
			
			File home=new File(path);
			
			String homeTree="Home";
			
			tree.addItem(homeTree);
			
			if(home.isDirectory()){
				String[] files=home.list();
				if(files!=null){
					FileComponent file=null;
					for(String fileName: files){
						file=new FileComponent(new File(path+File.separator+fileName));
						
						
						tree.addItem(file);
						tree.setItemIcon(file, file.getIcon());
						tree.setParent(file, homeTree);
						tree.setChildrenAllowed(file,file.isDirectory());
						
						current += 0.01;
						
						if (current>1.0){
							indicator.setValue(new Float(1.0)); 	
						}else{
							indicator.setValue(new Float(current));
						}
					}
					
					try{
						sleep(100);
					}catch (InterruptedException e) {
						// TODO: handle exception
					}
					
					indicator.setValue(new Float(1.0)); 
				}
			}
		}
	}
	*/

}
